AI Semantic Search in .NET MAUI DataGrid
The .NET MAUI DataGrid provides an AI semantic search feature that goes beyond traditional keyword matching. Unlike the standard text search, which looks for exact or partial text matches, the AI semantic search uses vector embeddings to match results based on conceptual similarity. This means users can find relevant data even when the search query doesn't contain the exact words present in the DataGrid cells.
For example, searching for "food" can match a row containing "Snacks" or "pizza, pretzels, popcorn" because the terms are semantically related.
AI semantic search requires an external AI embedding service (such as OpenAI, Azure AI, or a local ONNX model) to compute vector similarity.
Key Features
- AI Search View integration—Works with the DataGrid's AI Search View, which provides a UI for switching between text search and semantic search modes.
- Conceptual matching—Finds results based on meaning rather than exact text, improving relevance for natural-language queries.
- Custom matching logic—You control the matching through the
ProvideSearchMatchesActioncallback, allowing integration with any AI embedding service. - Filtering support—Optionally hides non-matching rows by setting the
ApplyFilterproperty. - Events—Provides
SearchStartingandSearchCompletedevents for intercepting and reacting to search operations.
How Semantic Search Works
The semantic search flow involves the following steps:
- The user enters a search query (through the
SearchTextproperty or the AI Search View). - The DataGrid iterates over each cell and invokes the
ProvideSearchMatchesActioncallback, passing aDataGridSemanticSearchCellProbeinstance. - The callback evaluates whether the cell value is semantically related to the search query (using an AI embedding service or custom logic) and sets the
IsMatchproperty on the probe. - The DataGrid highlights or filters the matching rows based on the results.
Getting Started with Semantic Search
To enable semantic search in the DataGrid, follow these steps:
-
Set the DataGrid's
IsAIEnabledproperty totrue. -
Define the DataGrid in XAML with
SemanticSearchSettingsandAISettings:
<telerik:RadDataGrid x:Name="dataGrid"
ItemsSource="{Binding Products}"
IsAIEnabled="True"
AutoGenerateColumns="False">
<telerik:RadDataGrid.SearchSettings>
<telerik:DataGridSearchSettings />
</telerik:RadDataGrid.SearchSettings>
<telerik:RadDataGrid.AISettings>
<telerik:DataGridAISettings ViewMode="Search"
SearchMode="SemanticSearch"
IsSuggestedPromptsVisible="False"
IsRecentPromptsVisible="False" />
</telerik:RadDataGrid.AISettings>
<telerik:RadDataGrid.SemanticSearchSettings>
<telerik:DataGridSemanticSearchSettings SearchStarting="OnSearchStarting"
SearchCompleted="OnSearchCompleted" />
</telerik:RadDataGrid.SemanticSearchSettings>
<telerik:RadDataGrid.Columns>
<telerik:DataGridTextColumn PropertyName="CategoryName"
HeaderText="Category" />
<telerik:DataGridTextColumn PropertyName="Description"
HeaderText="Description" />
</telerik:RadDataGrid.Columns>
</telerik:RadDataGrid>
- Add the
teleriknamespace:
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
- Set the
ProvideSearchMatchesActioncallback to supply the semantic matching logic:
// Set up the mock semantic search via the ProvideSearchMatchesAction.
// In a real scenario, this would call an AI embedding service.
this.dataGrid.SemanticSearchSettings.ProvideSearchMatchesAction = this.OnProvideSearchMatches;
- Implement the matching method. The method receives a
DataGridSemanticSearchCellProbeand setsIsMatchtotruefor cells that satisfy the semantic search criteria:
private void OnProvideSearchMatches(DataGridSearchProbe probe)
{
if (probe is DataGridSemanticSearchCellProbe cellProbe)
{
string cellText = cellProbe.CellValue?.ToString() ?? string.Empty;
// In a real scenario, this would call an AI embedding service.
cellProbe.IsMatch = LocalEmbeddingService.IsSemanticMatch(cellProbe.SearchText, cellText);
}
}
In a real application, replace the local matching logic with a call to an AI embedding service (such as OpenAI, Azure AI, or a local ONNX model) that computes vector similarity between the search query and the cell text.
- (Optional) Handle the
SearchStartingandSearchCompletedevents:
private void OnSearchStarting(object sender, DataGridSearchStartingEventArgs e)
{
// Called before searching starts.
// You can modify search terms or cancel the search here.
}
private void OnSearchCompleted(object sender, EventArgs e)
{
// Called when the semantic search operation completes.
}
- Define the data model:
public class Product
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
- Define the ViewModel:
public class ViewModel
{
public ViewModel()
{
this.Products = new ObservableCollection<Product>
{
new Product { CategoryId = 1, CategoryName = "Beverages", Description = "Soft drinks, coffees, teas, beers, and ales" },
new Product { CategoryId = 2, CategoryName = "Condiments", Description = "Sweet and savory sauces, relishes, spreads, and seasonings" },
new Product { CategoryId = 3, CategoryName = "Confections", Description = "Desserts, candies, and sweet breads" },
new Product { CategoryId = 4, CategoryName = "Dairy Products", Description = "Cheeses" },
new Product { CategoryId = 5, CategoryName = "Grains/Cereals", Description = "Breads, crackers, pasta, and cereal" },
new Product { CategoryId = 6, CategoryName = "Meat/Poultry", Description = "Prepared meats" },
new Product { CategoryId = 7, CategoryName = "Produce", Description = "Dried fruit and bean curd" },
new Product { CategoryId = 8, CategoryName = "Seafood", Description = "Seaweed and fish" },
new Product { CategoryId = 9, CategoryName = "Snacks", Description = "Chips, pretzels, and popcorn" },
new Product { CategoryId = 10, CategoryName = "Frozen Foods", Description = "Frozen vegetables, ice cream, and frozen dinners" },
new Product { CategoryId = 11, CategoryName = "Household", Description = "Cleaning products, paper goods, and other household items" },
new Product { CategoryId = 12, CategoryName = "Personal Care", Description = "Toiletries and personal care products" },
new Product { CategoryId = 13, CategoryName = "Health", Description = "Health and wellness products" },
new Product { CategoryId = 14, CategoryName = "Baby", Description = "Baby food, diapers, and other baby products" },
new Product { CategoryId = 15, CategoryName = "Pet Supplies", Description = "Food and supplies for dogs, cats, and other pets" },
new Product { CategoryId = 16, CategoryName = "Office Supplies", Description = "Office and school supplies" },
new Product { CategoryId = 17, CategoryName = "Automotive", Description = "Automotive parts and accessories" },
new Product { CategoryId = 18, CategoryName = "Books", Description = "Books across various genres and topics" },
new Product { CategoryId = 19, CategoryName = "Music", Description = "CDs, vinyl records, and music accessories" },
new Product { CategoryId = 20, CategoryName = "Movies", Description = "DVDs, Blu-rays, and streaming media" },
new Product { CategoryId = 21, CategoryName = "Electronics", Description = "Computers, phones, and other electronic devices" },
new Product { CategoryId = 22, CategoryName = "Clothing", Description = "Men's, women's, and children's apparel" },
};
}
public ObservableCollection<Product> Products { get; set; }
}
For a runnable example demonstrating the AI Semantic Search with mock service, see the SDKBrowser Demo Application and go to the DataGrid > Search category.
Using Semantic Search with AI Model
The DataGrid's AI Search View provides a built-in UI that allows users to switch between text search and semantic search modes. To enable semantic search as the default mode:
- Set the DataGrid's
IsAIEnabledproperty totrue. - Configure the
AISettingsproperty withViewModeset toSearchandSearchModeset toSemanticSearch. - Use the
ProvideSearchMatchesActionto specify search matches based on custom logic. - Configure an appropriate AI embedding service (such as OpenAI, Azure AI, or a local ONNX model) to compute vector similarity for semantic search.
For a runnable example demonstrating the Semantic Search with AI model, see the Controls Samples Demo Application and go to the DataGrid category.